﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using Fcdbas.Platform.Properties;
using Fcdbas.Core;
using Fcdbas.WinUI.Forms;

namespace Fcdbas.WinUI.Controls
{
    [Description("表示 Fcdbase 下拉控件，可以同时显示其他字段的内容的下拉")]
    [ToolboxBitmap(typeof(ComboBoxEdit))]
    [ToolboxItemFilter("System.Windows.Forms")]
    [DefaultBindingProperty("Text")]
    public partial class cRecSelComboBox : DevExpress.XtraEditors.XtraUserControl, IReference, IOwningDataSourceBinding, IReadOnly, IValidate, IControl
    {
        private PopupRecSelComboBox frmPopup;
        private cGridControl gridValue;
        private GridView gridView;
        private DataSourceBinding _dataSource;
        //事件定义
        private EventHandler onValueChanged;
        private EventHandler onGotFocus;
        private EventHandler onTextChanged;
        private EventHandler onLostFocus;
        public cRecSelComboBox()
        {
            InitializeComponent();
            frmPopup = new PopupRecSelComboBox();
            gridValue = frmPopup.gcValue;
            gridView = gridValue.MainView as GridView;
            frmPopup.Width = this.Width;
            frmPopup.Visible = false;
            this.txtValue.GotFocus += new EventHandler(txtValue_GotFocus);
            this.txtValue.TextChanged += new EventHandler(txtValue_TextChanged);
            this.txtValue.EditValueChanged += new EventHandler(txtValue_EditValueChanged);
            this.txtValue.LostFocus += new EventHandler(txtValue_LostFocus);
            gridView.RowCellClick += new RowCellClickEventHandler(gridView_RowCellClick);
            gridView.LostFocus += new EventHandler(gridView_LostFocus);
            this.txtValue.KeyPress += new KeyPressEventHandler(txtValue_KeyPress);
            this.txtValue.KeyDown += new KeyEventHandler(txtValue_KeyDown);
            this.txtValue.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(txtValue_EditValueChanging);
            this.gridView.KeyDown += new KeyEventHandler(gridView_KeyDown);
            this.gridView.KeyPress += new KeyPressEventHandler(gridView_KeyPress);
            this.txtValue.Enter += new EventHandler(txtValue_Enter);
        }

        void txtValue_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            //if(!this.DataSource.Session.IsDelete
            //this.DataSource.Session.RecordRefresh = true;
        }

        void gridView_LostFocus(object sender, EventArgs e)
        {
            this.frmPopup.Visible = false;
            this.DataSource.Session.RecordRefresh = false;
        }

        void txtValue_LostFocus(object sender, EventArgs e)
        {
            OnLostFocus(e);
        }
        private Color oldBackColor;
        protected override void OnLostFocus(EventArgs e)
        {
            if (onLostFocus != null)
            {
                onLostFocus.Invoke(this, e);
            }
            
           // frmPopup.Visible = false;
           // base.OnLostFocus(e);
        }
        void txtValue_EditValueChanged(object sender, EventArgs e)
        {
            OnValueChanged(e);
        }

        void txtValue_Enter(object sender, EventArgs e)
        {
            BaseForm parentForm = this.FindForm() as BaseForm;
            if (parentForm != null)
                parentForm.FireOnEnterEvent(this);

            base.OnEnter(e);
        }
        //private bool readOnly = true;
        #region IReadOnly 成员
        private Color oldForeColor = Color.Black;
        public bool ReadOnly
        {
            get
            {
                return this.txtValue.Properties.ReadOnly;
            }
            set
            {
                this.txtValue.Properties.ReadOnly = value;
                if(value)
                {
                    oldForeColor = this.txtValue.ForeColor;
                    this.txtValue.ForeColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.ReadOnlyColor;
                }
                else
                    this.txtValue.ForeColor = oldForeColor;
            }
        }

        #endregion
        private cLabel bindLabel;
        public cLabel BindLabel
        {
            get
            {
                return this.bindLabel;
            }
            set
            {
                this.bindLabel = value;
            }
        }
        public string Label
        {
            get
            {
                if (bindLabel != null)
                    return bindLabel.Text;
                else
                    return "";
            }

        }
        public new bool Validate()
        {
            DataSourceBinding ownerDataSourceBinding = GetOwningDataSourceBinding();
            if (DataColumn != null &&
                   DataColumn.Required &&
                   string.IsNullOrEmpty(this.Text))
            {
                WinUI.Forms.BaseForm form = this.FindForm() as WinUI.Forms.BaseForm;
                if (form != null)
                    form.ShowMessage("字段[%%1]不能为空！", DataColumn.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return ownerDataSourceBinding.Validate(this.DataColumn.ColumnName, this.Text);
        }
        public void AddColumn(DataItem di)
        {
            GridColumn col = new GridColumn();
            col.Caption = di.Caption;
            col.FieldName = di.ColumnName;
            this.gridView.Columns.Add(col);
        }
        private DataItem dataItem;
        /// <summary>
        /// 绑定的字段信息
        /// </summary>
        public DataItem DataColumn
        {
            get { return dataItem; }
            set
            {
                if (value == null)
                    return;
                dataItem = value;
                if (OwningDataSourceBinding != null && OwningDataSourceBinding.Updates == Platform.Enums.UpdatesSql.ReadOnly)
                    this.ReadOnly = true;
                this.txtValue.Properties.MaxLength = dataItem.Length;
                
                if (dataItem.DataType == typeof(decimal) ||
                   dataItem.DataType == typeof(double) ||
                   dataItem.DataType == typeof(Int16) ||
                   dataItem.DataType == typeof(int) ||
                   dataItem.DataType == typeof(Int64) ||
                   dataItem.DataType == typeof(Single) ||
                   dataItem.DataType == typeof(UInt16) ||
                   dataItem.DataType == typeof(uint) ||
                   dataItem.DataType == typeof(UInt64))
                {
                    if (string.IsNullOrEmpty(WinUI.Common.WinUtil.GetFormatMask(dataItem.FormatOption)))
                    {
                        this.txtValue.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None;
                        this.txtValue.Properties.Mask.EditMask = "";
                        this.txtValue.Properties.Mask.UseMaskAsDisplayFormat = true;
                    }
                    else
                    {
                        this.txtValue.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
                        this.txtValue.Properties.Mask.EditMask = string.IsNullOrEmpty(dataItem.CustomerFormat) ? WinUI.Common.WinUtil.GetFormatMask(dataItem.FormatOption) : dataItem.CustomerFormat;
                        this.txtValue.Properties.Mask.UseMaskAsDisplayFormat = true;
                    }


                }
                if (dataItem.DataType == typeof(string))
                {
                    if (dataItem.FormatOption.ToUpper() == "UPPERCASE")
                        this.txtValue.Properties.CharacterCasing = CharacterCasing.Upper;
                    else if (dataItem.FormatOption.ToUpper() == "LOWERCASE")
                        this.txtValue.Properties.CharacterCasing = CharacterCasing.Lower;
                }
            }
        }
        void gridView_KeyPress(object sender, KeyPressEventArgs e)
        {
            //退出
            if (e.KeyChar == 27)
            {
                this.DataSource.Session.RecordRefresh = false;
                this.frmPopup.Visible = false;
            }

            //Enter
            if (e.KeyChar == 13)
            {
                this.DataSource.Session.RecordRefresh = false;
                this.frmPopup.Visible = false;
            }
        }

        void gridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (gridView.RowCount <= 0)
                return;

            #region UP、Down、Tab

            if (e.KeyCode == Keys.Up)
            {
                
                OnValueChanged(EventArgs.Empty);
            }
            else if (e.KeyCode == Keys.Down)
            {
                
                //引发事件
                OnValueChanged(EventArgs.Empty);
            }
            else if (e.KeyCode == Keys.Tab)
            {
                this.DataSource.Session.RecordRefresh = false;
                this.frmPopup.Visible = false;
            }

            #endregion
        }
        public void SetValueToEntity()
        {
            DataSourceBinding dsb = this.DataSource as DataSourceBinding;

            if (dsb != null && this.DataColumn != null && dsb.Session != null && dsb.Session.IsNew)
            {
                IEntity en = dsb.Current as IEntity;
                if (en != null && this.DataColumn != null && this.DataColumn.ColumnName != null)
                {
                    en[this.DataColumn.ColumnName] = this.Text;
                }
            }
        }
        void txtValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.gridView.RowCount <= 0)
                return;
            if (!_buttonEnable)
                return;
            if (e.KeyCode == Keys.Up | e.KeyCode == Keys.Down)
                ShowPop();
        }
        #region  popup
        private void ShowPop()
        {

            ResizeForm(true);
            int nHeight = Screen.PrimaryScreen.WorkingArea.Bottom - (this.PointToScreen(this.Location).Y) - this.Height;
            int nWeight = Screen.PrimaryScreen.WorkingArea.Right - (this.PointToScreen(this.Location).X);
            Point point;
            if (nHeight < this.frmPopup.Height)
            {
                if (nWeight < this.frmPopup.Width)
                    point = this.Parent.PointToScreen(new Point(this.Right - this.frmPopup.Width, this.Top - frmPopup.Height));
                else
                    point = this.Parent.PointToScreen(new Point(this.Left, this.Top - frmPopup.Height));
            }
            else
            {
                if (nWeight < this.frmPopup.Width)
                    point = this.Parent.PointToScreen(new Point(this.Right - frmPopup.Width, this.Bottom));
                else
                    point = this.Parent.PointToScreen(new Point(this.Left, this.Bottom));
            }
            this.DataSource.Session.RecordRefresh = true;
            frmPopup.Show(this.FindForm());
            frmPopup.Location = point;
            gridView.Focus();
        }
        #endregion

        void txtValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            //退出
            if (e.KeyChar == 27)
            {
                this.DataSource.Session.RecordRefresh = false;
                this.frmPopup.Visible = false;
            }

            //Enter
            if (e.KeyChar == 13)
            {
                if (!this.frmPopup.Visible)
                    SendKeys.Send("{TAB}");
                this.DataSource.Session.RecordRefresh = false;
                this.frmPopup.Visible = false;
                return;
            }
        }

        void gridView_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            // GridView view = sender as GridView;
            //if(view == null)
            //    return;
            // DataSourceBinding dsb = view.DataSource as DataSourceBinding;
            //if(dsb == null)
            //    return;
            //if (dsb.List != null && dsb.List.Count > e.RowHandle && e.RowHandle >= 0)
            //{

            //    Fcdbas.Core.IEntity en = view.GetRow(e.RowHandle) as Fcdbas.Core.IEntity;
            //    if (en != null)
            //    {
            //        this.DataSource.
            //    }
            //}
            this.frmPopup.Visible = false;
            this.DataSource.Session.RecordRefresh = false;
            txtValue.Focus();
            txtValue.SelectAll();
            OnValueChanged(EventArgs.Empty);
           // OnTextChanged(EventArgs.Empty);
        }

        void txtValue_TextChanged(object sender, EventArgs e)
        {
            OnTextChanged(e);
        }

        void txtValue_GotFocus(object sender, EventArgs e)
        {
            OnGotFocus(e);
        }
        public DataSourceBinding DataSource
        {
            get
            {
                return _dataSource;
            }
            set
            {
                _dataSource = value;
                this.gridValue.DataSource = value;
            }
        }
        [Bindable(true)]
        public object EditValue
        {
            get
            {
                return this.txtValue.EditValue;
            }
            set
            {
                this.txtValue.EditValue = value;
            }
        }
        [Bindable(true)]
        public new string Text
        {
            get
            {
                return this.txtValue.Text;
            }
            set
            {
                if (this.DesignMode)
                    return;


                if (this.txtValue.Text != value)
                {
                }

                if (this.frmPopup != null)
                {
                    this.frmPopup.Visible = false;
                    this.DataSource.Session.RecordRefresh = false;
                }
            }
        }

        /// <summary>
        /// 输入框字符数约束
        /// </summary>
        [Category("自定义属性")]
        public int MaxLength
        {
            get
            {
                return this.txtValue.Properties.MaxLength;
            }
            set
            {
                txtValue.Properties.MaxLength = value;
            }
        }
        private string _displayMember;
        /// <summary>
        /// 显示成员
        /// </summary>
        [Category("自定义属性")]
        public string DisplayMember
        {
            get
            {
                return _displayMember;
            }
            set
            {
                _displayMember = value;
            }
        }
        public string ValueMember
        {
            get;
            set;
        }
        [Description("当文本改变时，引发事件！")]
        [Category("自定义事件")]
        public event EventHandler ValueChanged
        {
            add
            {
                onValueChanged += value;
            }
            remove
            {
                onValueChanged -= value;
            }
        }

        /// <summary>
        /// 值改变
        /// </summary>
        /// <param name="e">EventArgs</param>
        protected virtual void OnValueChanged(EventArgs e)
        {
            DataSourceBinding dsb = DataSource;

            if (onValueChanged != null)
            {
                onValueChanged.Invoke(this, e);
            }

            if (dsb != null)
            {
                if (dsb.Session.IsNew)
                {
                    IEntityList list = dsb.DataSource as IEntityList;
                    if (list != null && this.DataColumn != null && this.DataColumn.ColumnName != null)
                    {
                       // IEntity en = list.Find(this.DataColumn.ColumnName, this.Text);
                        IEntity cu = dsb.Current as IEntity;
                        if (cu != null )
                        {
                            cu[this.DataColumn.ColumnName] = this.Text;
                            return;
                        }

                    }

                }

                bool IsSave = false;
                if (dsb.Session.IsEdit || dsb.Session.IsDelete || dsb.Session.IsNew)
                    IsSave = true;
                if (IsSave)
                    return;

                if (dsb.Details != null)
                {
                    foreach (DataSourceBinding ds in dsb.Details)
                    {

                        if (ds.Session.IsDelete || ds.Session.IsEdit || ds.Session.IsNew)
                            IsSave = true;
                    }
                }
                

                Form frm = this.FindForm();
                if (IsSave)
                {
                    if (WinUI.Common.WinUtil.Translate.ShowMessage("数据已经修改，是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                       // Fcdbas.WinUI.Common.cSessionManager.DbTransactionBegin();
                        Fcdbas.Core.Windows.SalWindow.FireWindowActions(frm, Fcdbas.Platform.Const.PM_DataSourceSave, 1, dsb.Identify);
                    }
                    else
                    {
                        if (frm != null && frm is BaseForm)
                        {
                            ((BaseForm)frm).SetDeleteStatus(frm, false);
                        }
                        IEntity cn = dsb.Current as IEntity;
                        if (cn != null)
                        {
                            cn.Status = RecordStatus.Normal;
                            IEntity old = dsb.OldEntitys.Find(p => p["objid"].ToString() == cn["objid"].ToString());
                            if (old != null)
                                cn.SetAttr(old.GetAttr(false), false);
                            if (dsb.Details != null)
                            {
                                foreach (DataSourceBinding db in dsb.Details)
                                {
                                    //for (int i = 0; i < db.List.Count; i++)
                                    //{
                                    //    IEntity e1 = db.List[i] as IEntity;
                                    //    if (e1 != null)
                                    //    {
                                    //        if (e1.Status == RecordStatus.Delete)
                                    //            e1.Status = RecordStatus.Normal;
                                    //        else if (e1.Status == RecordStatus.Edit)
                                    //        {
                                    //            IEntity o1 = db.OldEntitys.Find(p => p["objid"].ToString() == e1["objid"].ToString());
                                    //            if (o1 != null)
                                    //            {
                                    //                e1.SetAttr(o1.GetAttr(false), false);
                                    //            }
                                    //        }
                                    //        else if (e1.Status == RecordStatus.New)
                                    //        {
                                    //            db.List.RemoveAt(i);
                                    //            i--;
                                    //        }
                                    //    }
                                    //}
                                    db.OldEntitys.Clear();
                                }
                            }
                        }
                        Fcdbas.Core.Windows.SalWindow.FireWindowActions(frm, Fcdbas.Platform.Const.AM_DataSourceMarkRollback, 1, dsb.Identify);
                    }
                }


                //if (!dsb.Session.IsNew && this.ItemIndex >= 0)
                //{

                //    dsb.Position = this.ItemIndex;
                //    if (dsb.Position != this.ItemIndex)
                //        this.ItemIndex = dsb.Position;
                //    RefreshOtherKey(false);
                //}
                if (frm != null && !dsb.Session.IsNew && this.Text != null && !string.IsNullOrEmpty(this.Text))
                {

                    IEntity en = dsb.Current as IEntity;
                    if (en == null)
                        return;
                    Dictionary<string, object> keys = new Dictionary<string, object>();
                    foreach (DataItem di in dsb.Columns)
                    {
                        if (!keys.ContainsKey(di.ColumnName) && en[di.ColumnName] != null)
                            keys.Add(di.ColumnName, en[di.ColumnName]);

                    }
                    Fcdbas.Core.Windows.SalWindow.FireWindowActions(frm, Fcdbas.Platform.Const.PM_DataRecordRefresh, DataSource, keys);
                }



            }
            IsEditing = true;
        }

        /// <summary>
        /// EventHandler
        /// </summary>
        // GotFocus
        [Description("获取焦点时，引发事件！")]
        [Category("自定义事件")]
        public new event EventHandler GotFocus
        {
            add
            {
                onGotFocus += value;
            }
            remove
            {
                onGotFocus -= value;
            }
        }

        /// <summary>
        /// 获得焦点
        /// </summary>
        /// <param name="e">EventArgs</param>
        protected new virtual void OnGotFocus(EventArgs e)
        {
            if (onGotFocus != null)
            {
                onGotFocus.Invoke(this, e);
            }
            oldBackColor = this.BackColor;
            this.BackColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.FocusColor;
        }

        public new event EventHandler LostFocus
        {
            add
            {
                onLostFocus += value;
            }
            remove
            {
                onLostFocus -= value;
            }
        }

        /// <summary>
        /// EventHandler
        /// </summary>
        //HelpFormTextChanged
        [Description("文本改变时，引发事件！")]
        [Category("自定义事件")]
        public new event EventHandler TextChanged
        {
            add
            {
                onTextChanged += value;
            }
            remove
            {
                onTextChanged -= value;
            }
        }

        /// <summary>
        ///// OnHelpFormTextChanged
        ///// </summary>
        ///// <param name="e">EventArgs</param>
        protected override void OnTextChanged(EventArgs e)
        {
            if (onTextChanged != null)
            {
                onTextChanged.Invoke(this, e);
            }
        }

        /// <summary>
        /// 设置弹出窗口大小，设置隐藏列
        /// </summary>
        /// <param name="bFirstFlag"></param>
        private void ResizeForm(bool bFirstFlag)
        {
            #region  调整弹出窗口宽度
            try
            {
                this.gridValue.Visible = true;
                int nWIDTH = Screen.PrimaryScreen.WorkingArea.Right - this.PointToScreen(this.txtValue.Location).X;
                int nWidth = 0;
                int tempWidth = 0;

                if (this.gridView.Columns.Count == 1)
                {
                    nWidth = this.gridView.Columns[0].Width;
                    if (nWidth < this.txtValue.Width)
                        this.gridView.Columns[0].Width = this.txtValue.Width;
                }
                else
                {
                    for (int i = 0; i < this.gridView.Columns.Count; i++)
                    {
                        if (!this.gridView.Columns[i].Visible)
                            continue;
                        nWidth += this.gridView.Columns[i].Width;
                    }

                    if (nWidth > (Screen.PrimaryScreen.WorkingArea.Right / 2))
                    {
                        nWidth = (Screen.PrimaryScreen.WorkingArea.Right / 2);
                    }
                    tempWidth = nWidth;
                }

                if (nWidth < this.txtValue.Width)
                    nWidth = this.txtValue.Width;
                else
                    //nWidth += 2;
                    nWidth += 12;  // 预留股东条的宽度

                //if (this.gridView.RowCount <= 0)
                //{
                //    this.frmPopup.Height = 154;
                //    this.frmPopup.Width = nWidth;
                //}
                //else if (this.gridView.RowCount < 6)
                //{
                //    this.frmPopup.Height = this.gridView.RowCount * this.gridView.RowHeight + 23;
                //    this.frmPopup.Width = nWidth;
                //}
                //else
                {
                    this.frmPopup.Height = 154;

                    if (nWidth == this.txtValue.Width)
                        this.frmPopup.Width = nWidth;
                    else
                        this.frmPopup.Width = nWidth + 12;
                }

               
            }
            catch (Exception ex)
            {
                Fcdbas.Core.Log.XTrace.WriteException(ex);
            }
            #endregion

        }
        private bool _buttonEnable = true;
        public void SetButtonEnable(bool enable)
        {
            _buttonEnable = enable;
            if (this.txtValue.Properties.Buttons.Count > 0)
            {
                this.txtValue.Properties.Buttons[0].Enabled = enable;
            }
        }
        private void cButtonEdit1_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (gridView.RowCount <= 0)
                return;

            if (this.frmPopup.Visible)
            {
                this.DataSource.Session.RecordRefresh = false;
                this.frmPopup.Visible = false;
            }
            else
            {
                this.txtValue.Focus();
                ShowPop();
            }
        }
        #region IReference 成员
        public bool IsDelete
        {
            set
            {
                if (value)
                {
                    Font font = new Font(this.Font, FontStyle.Strikeout);
                    this.Font = font;
                    oldForeColor = this.ForeColor;
                    this.ForeColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.DeleteColor;
                }
                else
                {
                    Font font = new Font(this.Font, FontStyle.Regular);
                    this.Font = font;
                    this.ForeColor = oldForeColor;
                }
            }
        }
        private bool _isEditing;
        public bool IsEditing
        {
            get
            {
                return _isEditing;
            }
            set
            {
                _isEditing = value;
                //if (value)
                //{
                //    this.oldForeColor = this.ForeColor;
                //    this.ForeColor = Fcdbas.WinUI.Common.AppEnvironment.AppOption.EditColor;
                //}
                if (!value)
                {
                    this.ForeColor = oldForeColor;
                }
            }
        }
        public bool IsReference
        {
            get
            {
                return !string.IsNullOrEmpty(Reference);
            }
        }
        public string Value
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = value;
            }
        }
        public string Reference
        {
            get
            {
                DataItem bindingItem = GetBindingItem();
                if (bindingItem != null)
                    return bindingItem.Reference;

                return null;
            }
        }
        private string columnName;
        public string Column
        {
            get
            {
                DataItem bindingItem = GetBindingItem();
                if (bindingItem != null)
                    return bindingItem.ColumnName;
                else
                    return columnName;
            }
            set
            {
                DataItem bindingItem = GetBindingItem();
                if (bindingItem != null)
                    bindingItem.ColumnName = value;
                columnName = value;
            }
        }

        public Type DataType
        {
            get
            {
                DataItem bindingItem = GetBindingItem();
                if (bindingItem != null)
                    return bindingItem.DataType;

                return null;
            }
        }

        public object ReferenceValue
        {
            get
            {
                return this.Text;
            }
        }

        public void SetValue(object value)
        {
            if (value == null ||
                value is DBNull)
                return;

            this.Text = value.ToString();
        }

        #endregion

        private DataItem GetBindingItem()
        {
            return DataColumn;
            //DataSourceBinding ownerDataSourceBinding = GetOwningDataSourceBinding();
            //if (ownerDataSourceBinding == null)
            //    return null;

            //DataItem bindingItem = ownerDataSourceBinding.GetDataItemByColumnName(this.DataBindings["Text"].BindingMemberInfo.BindingMember);
            //if (bindingItem != null)
            //    return bindingItem;

            //return null;
        }

        private DataSourceBinding GetOwningDataSourceBinding()
        {
            if (this.DataSource == null)
                return null;

            return this.DataSource ;
        }
        #region IOwningDataSourceBinding 成员
        private Binding _oldBinding;
        public Binding OldBinding
        {
            get
            {
                return _oldBinding;
            }
            set
            {
                _oldBinding = value;
            }
        }
        [Browsable(false)]
        public DataSourceBinding OwningDataSourceBinding
        {
            get { return GetOwningDataSourceBinding(); }
        }

        [Browsable(false)]
        DataSourceBinding IOwningDataSourceBinding.OwningDataSourceBinding
        {
            get { return OwningDataSourceBinding; }
        }

        [Browsable(false)]
        public DataItem BindingItem
        {
            get
            {
                return GetBindingItem();
            }
        }

        #endregion

        private void cRecSelComboBox_Load(object sender, EventArgs e)
        {
            if (this.DataSource != null && this.DataSource.Columns != null)
            {
                for (int i = 0; i < gridView.Columns.Count; i++)
                {
                    bool Found = false;
                    foreach (DataItem di in this.DataSource.Columns)
                    {
                        if (gridView.Columns[i].FieldName == di.ColumnName)
                        {
                            gridView.Columns[i].Visible = di.Queryable;
                            gridView.Columns[i].Caption = di.Caption;
                            Found = true;
                        }
                    }
                    if (!Found)
                        gridView.Columns[i].Visible = false;
                }
            }
            
        }
    }
}
